<?php 
// Header
$this->left_menu = 'menu/'.$this->params['controller'];
$this->set('title_for_layout', __("Tạo trang mới", true));
//$this->pageTitle = __("Tạo trang mới", true);
$params = array(
	'root'	=>	$this->params['named']['root']
);

if (isset($this->params['named']['related_group'])) {
	$params['related_group'] = $this->params['named']['related_group'];
}
if (isset($this->params['named']['related_id'])) {
	$params['related_id'] = $this->params['named']['related_id'];
}
if (isset($this->params['named']['back_url'])) {
	$params['back_url'] = $this->params['named']['back_url'];
}

if (isset($this->params['plugin'])) {
	$params['plugin'] = $this->params['plugin'];
} else {
	$params['plugin'] = null;
}

// -- Load CSS : Begin --
echo $this->Html->css('/editable_pages/css/add');
echo $this->Html->css('cake.form');
echo $this->Html->css('/editable_pages/css/jstree/tree_component');
// -- Load CSS : End  --

// Load Javascript
echo $this->Html->script('ckeditor/ckeditor_basic');

if (count($categories) || count($extended_trees)):
echo $this->Html->script('jstree/jquery.tree');
echo $this->Html->script('jstree/plugins/jquery.tree.checkbox');
echo $this->Html->script('jstree/libs/_lib');
endif;
?>
<script language="javascript">
$(document).ready(function () {
	<?php if (count($categories)):?>
	the_tree = $("#category_tree").tree({
		ui : {
			theme_name : "checkbox",
			theme_path : "<?php echo $html->url('/css/jstree/themes/checkbox/style.css')?>"
		},
		plugins : { 
			checkbox : { }
		}
	});
	<?php endif;?>
	var select_categories_div = $('div#select_categories');
	select_categories_div.dialog({
		resizable: false,
		width: 400,
		autoOpen: false,
		modal: true
	});

	$('a#select_categories_link').click(function() {
		select_categories_div.dialog('open');
	});

	// extended categories
	<?php foreach ($configs[$models['Page']['name']]['extended_categories'] as $key => $cat_id):?>
	the_tree_<?php echo $extendedCategory['id']?> = $("#category_tree_<?php echo $extendedCategory['id']?>").tree({
		ui : {
			theme_name : "checkbox",
			theme_path : "<?php echo $html->url('/css/jstree/themes/checkbox/style.css')?>"
		},
		plugins : { 
			checkbox : { }
		}
	});
	
	var select_categories_div_<?php echo $extendedCategory['id']?> = $('div#select_categories_<?php echo $extendedCategory['id']?>');
	select_categories_div_<?php echo $extendedCategory['id']?>.dialog({
		resizable: false,
		width: 400,
		autoOpen: false,
		modal: true
	});

	$('a#select_categories_link_<?php echo $extendedCategory['id']?>').click(function() {
		select_categories_div_<?php echo $extendedCategory['id']?>.dialog('open');
	});
	<?php endforeach;?>
	// extended categories

	var the_form = $('form#add_page_form');
	the_form.find('div.submit input[type="submit"]').click(function() {
		$('div#category_tree a.checked').each(function() {
			var check_id = $(this).parent().attr('node_id');
			the_form.append($("<input type='hidden' name='data[Category][]' value='"+check_id+"'/>"));
		});
		
		<?php foreach ($configs[$models['Page']['name']]['extended_categories'] as $key => $extendedCategory):?>
		$('div#category_tree_<?php echo $extendedCategory['id']?> a.checked').each(function() {
			var check_id = $(this).parent().attr('node_id');
			the_form.append($("<input type='hidden' name='data[Category][]' value='"+check_id+"'/>"));
		});
		<?php endforeach;?>
		//return false;
	});

	
	$('div#select_categories div.button_panel input[type="button"]').click(function() {
		select_categories_div.dialog('close');
	});
	<?php foreach ($configs[$models['Page']['name']]['extended_categories'] as $key => $extendedCategory):?>
	$('div#select_categories_<?php echo $extendedCategory['id']?> div.button_panel input[type="button"]').click(function() {
		select_categories_div_<?php echo $extendedCategory['id']?>.dialog('close');
	});
	<?php endforeach;?>
});

</script>
<?php 
$module_name = isset($category) ? $category['name'] : $configs[$models['Page']['name']]['global']['module_name'];
$plural = isset($category) ? $category['name'] : $configs[$models['Page']['name']]['global']['module_name'];
?>
<div class='right_content'>
<div class="title_left_sidebar">
	<div class="text_title">
		<?php echo sprintf(__("Tạo mới %s", true), $module_name)?>
	</div>
	<div class="create_edit">
		<div class="bd_title"></div>
		<div class="item_create">
			<div class="item_menu"><?php echo $html->image('toolbar/add.png', array('alt'=>__('Danh sách danh mục',true), 'align'=>'left')) ?></div>
			<div class="category_mn"><a id='add_facility_link' href="<?php echo $html->url($params + array('action'	=>	'index'))?>"><?php echo sprintf(__("Danh sách %s", true), $plural)?></a></div>
			<div class="clear"></div>
		</div>
		<div class="bd_title"></div>
	</div>
</div>

<?php echo $session->flash()?>

<div class='cake_form'>
<?php 
echo $form->create($models['Page']['name'], array(
	'url'	=>	
	$params + array(
		'controller'	=>	$this->params['controller'],	
		'action'		=>	'add'
	),
	'id'		=>	'add_page_form',
	'enctype'	=>	'multipart/form-data'
));

echo $form->input('title', array(
	'label'	=>	__("Tiêu đề: ", true),
	'error'	=>	__("Tiêu đề trang không được để trống", true)
));

if ($configs['Manage']['add']['show_alias']) {
	echo $form->input('alias', array('label'	=>	__("Alias: ", true)));
} else {
	echo $form->input('alias', array('label'	=>	__("Alias: ", true), 'type' => 'hidden'));
}

if ($configs[$models['Page']['name']]['image']['show']):
$options = array(
	//'name'				=>	$this->data['Article']['image'],
	'name'				=>	'',
	'path'				=>	'files/categories/',
	'field_name'		=>	'image',
	'label'				=>	__('&nbsp;',true),
	'legend_title'		=>	__('Hình ảnh',true),
	//'delete_image_link'	=>	$html->url('/news/manage_news/delete_image/'.$this->data['Article']['id'])
	'delete_image_link'	=>	''
);
echo $html->form_image($options,array());
endif;
?>
<?php if ($configs[$models['Page']['name']]['description']['show']):?>
<div class="input">
<table cellpadding='0' cellspacing='0' border='0' width='100%'>
<tr>
	<td class='label' valign='top'><?php __("Mô tả: ")?></td>
	<td>
		<?php
		echo $form->input('description', array('label'	=>	false, 'div'=>false, 'type'=>'textarea', 'value'	=>	$configs[$models['Page']['name']]['description']['default_content'])); 
		
		if (!isset($configs[$models['Page']['name']]['description']['use_rich_text']) || $configs[$models['Page']['name']]['description']['use_rich_text']):
		echo $wysiwyg->render("{$models['Page']['name']}.description", 'ckeditor', $configs[$models['Page']['name']]['description_config']);
		endif;
		
		?>
	</td>
</tr>
</table>
</div>
<?php endif;?>

<?php if ($configs[$models['Page']['name']]['content']['show']):?>
<div class="input">
<table cellpadding='0' cellspacing='0' border='0' width='100%'>
<tr>
	<td class='label' valign='top'><?php __("Nội dung: ")?></td>
	<td>
		<?php
		echo $form->input('content', array('label'	=>	false, 'div'=>false, 'type'=>'textarea', 'value' => $configs[$models['Page']['name']]['content']['default_content'])); 
		
		if (!isset($configs[$models['Page']['name']]['description']['use_rich_text']) || $configs[$models['Page']['name']]['description']['use_rich_text']):
		echo $wysiwyg->render("{$models['Page']['name']}.content", 'ckeditor', $configs[$models['Page']['name']]['content_config']);
		endif;
		?>
	</td>
</tr>
</table>
</div>
<?php endif;?>
			
<?php if (count($categories)):?>
<div class='input text'>
	<label>&nbsp;</label>
	[ <a href='javascript:void(0)' id='select_categories_link'><?php __("Bấm vào đây để chọn danh mục")?></a> ]
</div>
<?php else:?>
<?php echo $form->input('Category', array('type'=>'hidden', 'value'=>$category_root_id, 'name'=>"data[Category][]"))?>
<?php endif;?>

<?php foreach ($configs[$models['Page']['name']]['extended_categories'] as $key => $extendedCategory):?>
<div class='input text'>
	<label>&nbsp;</label>
	[ <a href='javascript:void(0)' id='select_categories_link_<?php echo $extendedCategory['id']?>'><?php echo $extendedCategory['title']?></a> ]
</div>
<?php endforeach;?>
<div id='dialogs'>
<div id='select_categories' title="<?php __("Chọn danh mục")?>">
<?php 
$settings = array(
	'data'			=>	$categories,
	'expand_all'	=>	true,
	'attributes'	=>	array(
		'id'	=>	'category_tree',
		'class'	=>	'demo'
	),
	'fields'		=>	array(
		'text'	=>	'name',
		'id'	=>	'id'
	),
	'model'	=>	$models['Category']['name']
);
echo $this->element('tree/jstree_checkbox', array('settings'	=>	$settings));?>
<div align='center' class='button_panel'>
	<input type='button' class='button' value='<?php __(" Đóng ")?>'/> 
</div>
</div>

<?php foreach ($configs[$models['Page']['name']]['extended_categories'] as $key => $extendedCategory):?>
<div id='select_categories_<?php echo $extendedCategory['id']?>' title="<?php __("Select Categories")?>">
<?php 
$settings = array(
	'data'			=>	$extended_trees[$extendedCategory['id']],
	'expand_all'	=>	true,
	'attributes'	=>	array(
		'id'	=>	'category_tree_'.$extendedCategory['id'],
		'class'	=>	'demo'
	),
	'fields'		=>	array(
		'text'	=>	'name',
		'id'	=>	'id'
	),
	'model'	=>	$extendedCategory['model']
);
echo $this->element('tree/jstree_checkbox', array('settings'	=>	$settings));?>
<div align='center' class='button_panel'>
	<input type='button' class='button' value='<?php __(" Đóng ")?>'/> 
</div>
</div>
<?php endforeach;?>

</div>
<?php
// --- Load Css if supported ---- //
echo $this->element('extra_fields');
// --- Load Css if supported ---- //
?>
<?php
echo $form->end(sprintf(__('Tạo mới %s', true), $module_name));
?>
</div>
</div>